package dbexp.model;

import java.util.ArrayList;
import java.util.List;

import dbexp.model.db.DbOT;

/**
 * @author Michał Skrzyński
 * @email michal_skrzynski@o2.pl
 */
public class Table extends DbObject {

	private static final long serialVersionUID = 1L;
	public static final String LABEL = "tabele";
	public static final String DEF_ICON = "table-icon";
	private List<Column> kolumny = new ArrayList<Column>();

	public Table() {
		super(LABEL, DbOT.TABLES);
		setIcons("tables-icon");
	}

	public Table(String tname) {
		super(tname, DbOT.TABLE);
		setIcons(DEF_ICON, "tables-icon");
		set("leaf", true);
	}

	public Table(String tname, String sch) {
		super(tname, sch, DbOT.TABLE);
		setIcons(DEF_ICON, "tables-icon");
		set("leaf", true);
	}

	public boolean tableIsLock() {
		return is("lock");
	}

	public void setTableLock(String lock) {
		set("lock", ((lock != null && lock.equalsIgnoreCase("DISABLED")) ? true
				: false));
	}

	public boolean isTemporary() {
		return is("temporary");
	}

	public void setTemporary(String t) {
		set("temporary",
				((t != null && t.equalsIgnoreCase("Y")) ? true : false));
	}

	public String getDegree() {// stan
		return get("degree");
	}

	public void setDegree(String d) {
		set("degree", d);
	}

	public String getTableSpace() {
		return get("tablespace");
	}

	public void setTableSpace(String tablespace) {
		set("tablespace", tablespace);
	}

	public String getSchema() {
		return get("schema");
	}

	public void setSchema(String schema) {
		set("schema", schema);
	}

	public String getTableName() {
		return getName();
	}

	public void setTableName(String tableName) {
		setName(tableName);
	}

	public void setColumns(List<Column> cols) {
		this.kolumny = cols;
		set("cols", cols);
	}

	public List<Column> getColumns() {
		return this.kolumny;
	}

	public void addColumn(Column col) {
		this.kolumny.add(col);
	}

	public Column getCols(int i) {
		return this.kolumny.get(i);
	}

	public String getQuery(int operation) {
		if (operation == CREATE) {
			return getQuery("CREATE ");
		} else if (operation == ADD_COLUMN) {
			return getAddColumnQuery("ALTER ");
		} else if (operation == ALTER_COLUMN) {
			return getAlterColumnQuery("ALTER ");
		} else if (operation == DROP_COLUMN) {
			return getDropColumnQuery("ALTER ");
		} else if (operation == DELETE) {
			return "DROP TABLE " + getObjectNameForQuery() + "";
		} else if (operation == INSERT_RECORD) {
			return "INSER INTO " + getObjectNameForQuery() + "";
		} else if (operation == UPDATE_RECORD) {
			return "UPDATE " + getObjectNameForQuery() + " SET";
		} else if (operation == DELETE_RECORD) {
			return "DELETE FROM TABLE " + getObjectNameForQuery() + " WHERE";
		}
		return oneFromDual();
	}

	private String getAddColumnQuery(String string) {
		String out = string;
		int columnSize = getColumns().size();
		if (requiredFieldIsNotNull()) {
			out += "TABLE " + getObjectNameForQuery() + " ADD(";
			for (int i = 0; i < columnSize; i++) {
				Column c = getColumns().get(i);
				if (c.getAction() == CREATE) {
					out += c.getName() + " " + c.getFormattedDataType() + " "
							+ ((c.isNullable()) ? "" : " NOT NULL");
				}
				if (i < (columnSize - 1)) {
					out += ",";
				}
				out += "\n";
			}
			out += ")";
		}
		return out;
	}

	private String getAlterColumnQuery(String string) {
		String out = string;
		int columnSize = getColumns().size();
		if (requiredFieldIsNotNull()) {
			out += "TABLE " + getObjectNameForQuery() + " MODIFY(";
			for (int i = 0; i < columnSize; i++) {
				Column c = getColumns().get(i);
				if (c.getAction() == ALTER) {
					out += c.getName()
							+ " "
							+ c.getFormattedDataType()
							+ " "
							+ ((c.isNullable()) ? "" : " NOT NULL")
							+ ((c.getDefault().trim().length() > 0) ? " DEFAULT "
									+ c.getDefault()
									: "");
				}
				if (i < (columnSize - 1)) {
					out += ",";
				}
				out += "\n";
			}
			out += ")";
		}
		return out;
	}

	private String getDropColumnQuery(String string) {
		String out = string;
		int columnSize = getColumns().size();
		if (requiredFieldIsNotNull()) {
			boolean any = false;
			out += "TABLE " + getObjectNameForQuery() + " DROP (";
			for (int i = 0; i < columnSize; i++) {
				Column c = getColumns().get(i);
				if (c.getAction() == DELETE) {
					out += "," + c.getName();
					any = true;
				}
			}
			out += ")";
			out = out.replaceFirst(",", "");
			if (!any) {
				return oneFromDual();
			}
		}
		return out;
	}

	private String getQuery(String string) {
		String out = string;
		int columnSize = getColumns().size();
		if (requiredFieldIsNotNull()) {
			out += "TABLE " + getObjectNameForQuery() + " (\n";
			for (int i = 0; i < columnSize; i++) {
				Column c = getColumns().get(i);
				out += "\t" + c.getName() + " " + c.getFormattedDataType()
						+ " " + ((c.isNullable()) ? "" : " NOT NULL");
				if (i < (columnSize - 1)) {
					out += ",";
				}
				out += "\n";
			}
			out += ");";
		}
		return out;
	}

}
